﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SMS.Business;
using SMS.DAL.Providers;
using SMS.Business.Stocks;
using SMS.DAL;
using SMS.Business.Items;

namespace SMS.Items
{
    public partial class UpdateItem : System.Web.UI.Page
    {
        string QryItemID;
        User CurrentUser
        {
            get
            {
                return (User)Session["CurrentUser"];
            }
        }
        List<StockItem> stockItems
        {
            set
            {
                if (ViewState["StockItems"] == null)
                    ViewState["StockItems"] = new List<StockItem>();
                else
                    ViewState["StockItems"] = value;

            }
            get
            {
                if (ViewState["StockItems"] != null)
                    return (List<StockItem>)ViewState["StockItems"];
                else
                    return new List<StockItem>();
            }
        }

        private void BindDlStockItems()
        {
            StocksBusiness stockBus = new StocksBusiness();
            StockUserSearchCriteria stockUserSearch = new StockUserSearchCriteria()
            {
                UserID = CurrentUser.ID
            };
            List<Stock> stocks = stockBus.GetByUserID(stockUserSearch);
            stockItemBus = new StockItemsBussiness();
            QryItemID = Request.QueryString["ItemID"];
            stockItems = new List<StockItem>();
            stockItems = stockItemBus.GetByStocks(stocks, int.Parse(QryItemID));
            dlStockItems.DataSource = stockItems;
            dlStockItems.DataBind();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDlStockItems();
            }
        }

        protected void dlStockItems_CancelCommand(object source, DataListCommandEventArgs e)
        {
            dlStockItems.EditItemIndex = -1;
            BindDlStockItems();
        }

        protected void dlStockItems_DeleteCommand(object source, DataListCommandEventArgs e)
        {
            int id = (int)dlStockItems.DataKeys[e.Item.ItemIndex];
            stockItemBus = new StockItemsBussiness();
            stockItemBus.Delete(id);
            BindDlStockItems();
        }

        protected void dlStockItems_EditCommand(object source, DataListCommandEventArgs e)
        {
            dlStockItems.EditItemIndex = e.Item.ItemIndex;
            BindDlStockItems();
        }

        protected void dlStockItems_ItemCommand(object source, DataListCommandEventArgs e)
        {

        }

        StockItemsBussiness stockItemBus;
        protected void dlStockItems_UpdateCommand(object source, DataListCommandEventArgs e)
        {
            ItemBusiness itemBus = new ItemBusiness();
            QryItemID = Request.QueryString["ItemID"];
            int id = (int)dlStockItems.DataKeys[dlStockItems.EditItemIndex];
            stockItemBus = new StockItemsBussiness();
            StockItem updatedStockItem = new StockItem(){ ID = id, StockID=stockItems[e.Item.ItemIndex].StockID };
            TextBox txtCount = (TextBox)dlStockItems.Items[e.Item.ItemIndex].FindControl("txtCount");
            int _count = int.Parse(txtCount.Text);
            updatedStockItem.Count = _count;
            TextBox txtMinCount = (TextBox)dlStockItems.Items[e.Item.ItemIndex].FindControl("txtMinCount");
            int _minCount = int.Parse(txtMinCount.Text);
            updatedStockItem.MinCount = _minCount;
            Item updatedItem = new Item() { ID = int.Parse(QryItemID) };
            TextBox txtManfCountry = (TextBox)dlStockItems.Items[e.Item.ItemIndex].FindControl("txtManfCountry");
            string _manfCountry = txtManfCountry.Text;
            updatedItem.ManfCountry = _manfCountry;
            TextBox txtMark = (TextBox)dlStockItems.Items[e.Item.ItemIndex].FindControl("txtMark");
            updatedItem.Mark = txtMark.Text;
            TextBox txtName = (TextBox)dlStockItems.Items[e.Item.ItemIndex].FindControl("txtName");
            updatedItem.Name = txtName.Text;
            TextBox txtPartNumber = (TextBox)dlStockItems.Items[e.Item.ItemIndex].FindControl("txtPartNumber");
            updatedItem.PartNumber = txtPartNumber.Text;
            TextBox txtSellingPrice = (TextBox)dlStockItems.Items[e.Item.ItemIndex].FindControl("txtSellingPrice");
            updatedItem.SellingPrice = decimal.Parse(txtSellingPrice.Text);
            TextBox txtBarCode = (TextBox)dlStockItems.Items[e.Item.ItemIndex].FindControl("txtBarCode");
            updatedItem.BarCode = txtBarCode.Text;
            itemBus.Update(updatedItem);
            stockItemBus.Update(updatedStockItem);
            dlStockItems.EditItemIndex = -1;
            BindDlStockItems();
        }
    }
}
